package cn.uyunsky.platform.data;

import java.util.HashMap;
import java.util.Map;

public class UDataTool {

    public static final String ERROR_CODE = "9999";
    public static final String ERROR_MSG = "服务执行异常";

    public static UData createResult(boolean success) {
        UHead head = new UHead();
        head.setSuccess(success);
        head.setMsg(success ? "" : ERROR_MSG);
        head.setErrorcode(success ? "" : ERROR_CODE);
        UData data = new UData();
        data.setHead(head);
        return data;
    }

    /**
     * <p>
     * Description: 仅仅创建一个头数据
     * </p>
     * 参数说明:
     *
     * @param success 是否陈成功
     * @param msg     响应信息
     * @return
     */
    public static UData createResult(boolean success, String errorcode, String msg) {
        UHead head = new UHead();
        head.setSuccess(success);
        head.setMsg(msg == null ? "" : msg);
        head.setErrorcode(errorcode);
        UData data = new UData();
        data.setHead(head);
        return data;
    }

    /**
     * <p>
     * Description: 创建一个成功的数据体
     * </p>
     * 参数说明:
     *
     * @param body
     * @return
     */
    public static UData createSuccessResult(Map<String, Object> body) {
        UHead head = new UHead();
        head.setSuccess(true);
        head.setErrorcode("");
        head.setMsg("");
        UData data = new UData();
        data.setHead(head);
        data.setBody(body);
        return data;
    }


    /**
     * 根据参数数组创建一个UData
     *
     * @param keys
     * @param values
     * @return
     */
    public static UData createOneUData(String[] keys, Object[] values) {
        UHead head = new UHead();
        head.setSuccess(true);
        head.setErrorcode("");
        head.setMsg("");

        UData data = new UData();
        data.setHead(head);

        Map body = new HashMap();
        for (int i = 0; i < keys.length; i++) {
            body.put(keys[i], values[i]);
        }
        data.setBody(body);
        return data;
    }

    /**
     * 根据参数数组创建一个UData
     *
     * @param key
     * @param value
     * @return
     */
    public static UData createOneUData(String key, Object value) {
        return createOneUData(new String[]{key},new Object[] {value});
    }

}
